Tutorial 1 CSC 201 Java Programming Concepts عؾادئماظربجمةمبادؿكدامماجلاصا
م- م- م- م- م- Chapter 1 1. What is Java? 2. Why Learn Java? a. Java Is Platform Independent b. Java is Easy to learn 3. Programming In Java a. Getting and installing the software b. Creating a Java program 4. Comments 5. Errors a. Syntax Errors b. Logic Errors Summary عامػيماجلاصا ملاذامغؿعؾمماجلاصا اجلاصامالمتعؿؿدمسؾىمبقؽةماظؿشغقل اجلاصامدفؾةماظؿعؾم اظربجمةمبادؿكدامماجلاصا احلصولمسؾىماملشغلموتؾقؿه باءمبرغاعجمبؾغةماجلاصا اظؿعؾقؼات األخطاء أخطاءميفمضواسدماظؽؿابة أخطاءمعطؼقة 1 2 3 4 5
1.What is Java? Java is an object oriented programming language. It has been developed by Sun Microsystems. Java is a high-level language. The Java language goal was to be a small, simple, and portable programming language. ظغةماجلاصامػيمظغةممبرجمةمػقؽؾقةم)باؤػاماألداديمػوماظػؽةممclass (وػيمعنمظغاتمامللؿويماظعاظيم )عػفوعةمظؾؼارئ(موتعؿربمظغةمبرجمةمحمؿوظةمميؽنمادؿكداعفامسؾىماألجفزةموأغظؿةماظؿشغقلماملكؿؾػة.م (Tip: Everything that we have in life is almost an object. A car for example is an object. The TV set is also an object. Now Java is very powerful in representing these types of objects)
2.Why Learn Java? Java is Platform Independent اجلاصامالمتعؿؿدمسؾىمبقؽةماظؿشغقلم ميؽنمتػقذمبرغاعجماجلاصام سؾىمأيمجفازمعؾتمسؾقةمآظةماجلاصاماالصرتاضقةم) JVM ( A platform is the hardware or software environment in which a program runs. Platform independence is one of the most significant features where you can write your code once and run it anywhere that supports a Java virtual machine (JVM). That means that as long as a computer has a JVM, the same program written in the Java programming language can run on Windows 2000, Linux, a Solaris workstation, or on an imac, briefly, on any operating system. This feature will be clearly understood if you know that with most programming languages, you either compile or interpret a program so that you can run it on your computer. Compilations means to translate the file into machine code. Interpretation means to execute the compiled file using another program.
The Java programming language is unusual in that a program is both compiled and interpreted. With the compiler, first you translate a program into an intermediate language called Java "bytecode". The interpreter runs each Java bytecode instruction on the computer. Compilation happens just once and interpretation occurs each time the program is executed. The following figure illustrates how this works.
Java bytecodes help make "write once, run anywhere" possible. You can compile your program into bytecodes on any platform that has a Java compiler. The bytecodes can then be run on any implementation of the Java VM. Java is Easy to Learn ظغةماجلاصامدفؾةماظؿعؾمموضدمصؿؿتمظؿؽونمدفؾةماظؽؿابةم دفؾةماظؿفؿقع دفؾةماظؿصققح,مسؾىماظرشممعنمبلارؿفامصإغفامظغةمضوؼةموعرغة Java was constructed to be a simple programming language. Its initial design goals was for it to be a small and simple language that would be easy to write, easy to compile, and easy to debug. Though its simplicity it is still a very powerful and flexible programming language.
3. Programming in Java اظربجمةمبادؿكدامماجلاصا Creating a Java program إغشاءمبرغاعجمجاصا Create a source file. A source file contains text, written in Java programming language that you and other programmers can understand. You can use any text editor to create and edit source عؾفماملصدرم)طودماظربغاعج(محيؿويمسؾىمغصمعؽؿوبمبؾغةماجلاصامؼلؿطقعمأيمعربعجمأنمؼػفؿه.مميؽكمبادؿكداممأيمحمررم files. غصمإغشاءمأومتعدؼلمعؾفمعصدر /* This program simply displays "Hello World!" on the screen */ class Hello ػذاماظربغاعجمبؾلارةمؼعرضمرداظةمسؾىماظشاذة { public static void main(string[] args) { System.out.println("Hello World!"); } } Note: Java Language is case sensitive, "A" is different than "a". Write the above code and be careful with capital and small letters. اجلاصامحلادةمحلاظةماألحرفم احلرفماظؽؾريمم A مخيؿؾفمسنماحلرفماظصغريم a طنمحذرامسدمطؿابةماظؽود
4. Comments اظؿعؾقؼاتمسؾىماألواعرميفماظربغاعجم جزءمعنماظؽودموظؽفامعفؿؾةمعنماملرتجمممأيم اغفامالمتػذمصائدتفامصؼطماظؿعؾققمسؾىمخطوةمأومذرحمخطوة A comment is a piece of text in a source file that is ignored by the compiler. Comments are used to allow the programmer to express any comment that he/she needs to write about the program. The Java language supports two kinds of comments: /* This is the first kind of comment. It allows you to write comments for any number of lines. A single comment continues until it is terminated with the closing characters. */ // This is the second kind of comment. // It only covers one line. تعؾققمعؿعددماألدطرمممؼؾدأمبمممممم*/م وؼؿفيمبمممممم/* تعؾققمدطرمواحدمؼؾدأمبمممممم//م
5. Errors There are two types of errors: syntax and logic errors. a. Syntax Errors االخطاءم ؼوجدمغوسنيمعنماألخطاءميفماظربغاعج A syntax error is an error in the syntax of the code written. Such an error will cause a compile-time error that prevents the program from running. Missing a semicolon System.out.println("Hello") Unclosed/Unopened Quotation Marks System.out.println("Hello); System.out.println(Hello"); Class Name different from file name Try writing the following code: public class Hello() { public static void main(string[] args) { System.out.println("Hello"); } } Then save the file as a different name than "Hello.java". This will cause a compile-time error because the class and file name do not match. أم خطأميفمضواسدماظؽؿابةمؼلؾبمخطأمأثاءمترمجةماظربغاعجم وميعمتػقذه م عال:مغلقانماظػاصؾةماملؼورةميفمغفاؼةمأيممجؾةم عال:مغلقانمصؿحمأومشؾقمسالعةماظؿصقصم عال:مادمماظؽالسمخمؿؾفمسنمادمماملؾفماملشأمظؾربغاعج
5. Errors a. Syntax Errors أم خطأميفمضواسدماظؽؿابة م عال:ماالخطاءماإلعالئقةم Spelling mistakes public static vod main(string[] args) Case Sensitivity public static void main(string[] args) Un-terminated Comment /* The following statement displays Hello System.out.println("Hello"); The previous will cause a compile-time error because the comment has not been terminated. Using an un-initialized variable public static void main(string[] args) { int x; int y = x +5; } This program will give a compile-time error because the variable x has not been initialized before using it. عال:ماحللادقةمحلاظةماألحرفم عال:مبداؼةمتعؾققموسدممشؾؼهم م عال:مادؿكداممعؿغريمشريمععؾنمسهمأومظقسمظهمضقؿةمعؾدئقة
b. Logic Errors بشؽلمصققحم)خطاميفمعففقةماحللموظقسميفمطؿابةماظربغاعج( A logic error is an error that will not cause an error message. It occurs when the program does not perform what the programmer intended for it to do. With this type of error, the program will compile as well as run, but it will give undesired results. عال:مرؾاسةماظرداظةمبشؽلمخطأميفماظربغاعجماملوضحمدابؼا error: The following is an example of a logic System.out.println("Hell"); The previous statement was meant to print the word "Hello". Instead it will print "Hell". Summary ب خطأمعطؼيمالمؼلؾبمزفورمرداظةمخطأموباظؿاظيمؼػذماظربغاعجموظؽهمالمؼؤديماظعؿؾةماملطؾوبةم غاضشامباخؿصارمظغةماجلاصامطؾغةمبرجمةموممقزاتفاموطقفمميؽكماحلصولمسؾىماظربغاعجماملشغلم وتؾقؿه موسرضامطقفمميؽنمطؿابةمبرغاعجمجاصاموترمجؿهموتػقذه We have briefly discussed Java as a programming language and its advantages. We talked about how to get an install the java software development kit or JSDK. We also have demonstrated how to create, compile and run program.